/*
 * @Author: wwssaabb 
 * @Date: 2021-07-Su 05:58:18 
 * @Last Modified by:   wwssaabb 
 * @Last Modified time: 2021-07-Su 05:58:18 
 */

const router=require('koa-router')()
const {User}=require('../model/user')


//路由添加前缀
router.prefix('/api')

//获取个人信息，传_id
router.get('/info',async (ctx,next)=>{
  console.log(ctx.request.query)
  let {_id}=ctx.request.query
  let res=await User.findOne({_id},{password:0})
  ctx.body=res
})

//获取用户列表，传page，size
router.get('/list',async (ctx,next)=>{
  const data={}
  let {page,size}=ctx.request.query
  size=parseInt(size?size:20)
  let totalCount=await User.find({},{password:0}).count()
  let totalPage=Math.ceil(totalCount/size)
  let res=await User.find({},{password:0}).skip((page-1)*size).limit(size)
  data.list=res
  ctx.body={data,totalCount,totalPage,currentPage:page,size}
})

module.exports=router

